#ifndef ASYNCWEBSERVERRESPONSEIMPL_H_
#define ASYNCWEBSERVERRESPONSEIMPL_H_

#include <vector>

#define CONFIG_TEMPLATE_PLACEHOLDER     '%'
#define CONFIG_TEMPLATE_PARAM_NAME_LENGTH   32

// 处理基本的HTTP响应
class AsyncBasicResponse: public AsyncWebServerResponse {
private:
    std::string  m_content;  // 剩余要发送的内容（HTML/JSON等）

public:
    AsyncBasicResponse(int code, const std::string &contentType = std::string(), const std::string &content = std::string());
    void _respond(AsyncWebServerRequest* req);
    size_t _ack(AsyncWebServerRequest* req, size_t len, uint64_t time);
    bool _sourceValid() const
    {
        return true;
    }
};

class AsyncAbstractResponse : public AsyncWebServerResponse {
private:
    std::string             m_head;     // 存储响应头部信息
    std::vector<uint8_t>    m_cache;    // 用于缓存响应的数据
    size_t  m_readDataFromCacheOrContent(uint8_t* data, const size_t len);
    size_t  m_fillBufferAndProcessTemplates(uint8_t* buf, size_t maxLen);
protected:
    AwsTemplateProcessor    m_callback; // 模板处理器回调，处理动态内容生成、模板替换
public:
    AsyncAbstractResponse(AwsTemplateProcessor cb = nullptr);
    void    _respond(AsyncWebServerRequest* req);
    size_t _ack(AsyncWebServerRequest* req, size_t len, uint64_t time);
    bool _sourceValid() const
    {
        return false;
    }
    virtual size_t _fillBuffer(uint8_t *buf __attribute__((unused)), size_t maxLen __attribute__((unused)))
    {
        return 0;
    }
};

class AsyncFileResponse : public AsyncAbstractResponse {
private:
    FILE*           m_content;      // 文件句柄
    std::string     m_path;         // 客户端请求真实资源路径
    void m_setContentType(const std::string &path);
public:
    AsyncFileResponse(const std::string &path, const std::string &contentType = std::string(), bool download = false, AwsTemplateProcessor cb = nullptr);
    ~AsyncFileResponse();
    bool _sourceValid() const
    {
        return m_content != nullptr;
    }
    virtual size_t _fillBuffer(uint8_t* buf, size_t maxLen) override;
};

class AsyncCallbackResponse: public AsyncAbstractResponse {
private:
    AwsResponseFiller   m_content;      // 用于填充响应内容的回调
    size_t              m_filledLength; // 已填充到缓冲区的总字节数
public:
    AsyncCallbackResponse(const std::string &contentType, size_t len, AwsResponseFiller cb, AwsTemplateProcessor templateCb = nullptr);
    bool _sourceValid() const
    {
        return m_content != nullptr;
    }
    virtual size_t _fillBuffer(uint8_t* buf, size_t maxLen) override;
};

class AsyncChunkedResponse: public  AsyncAbstractResponse {
private:
    AwsResponseFiller m_content;
    size_t m_filledLength;
public:
    AsyncChunkedResponse(const std::string &contentType, AwsResponseFiller cb, AwsTemplateProcessor templateCb = nullptr);
    bool _sourceValid() const
    {
        return m_content != nullptr;
    }
    virtual size_t _fillBuffer(uint8_t* buf, size_t maxLen) override;
};

class AsyncProgmemResponse: public AsyncAbstractResponse {
private:
    const uint8_t*  m_content;
    size_t  m_readLength;
public:
    AsyncProgmemResponse(int code, const std::string &contentType, const uint8_t * content, size_t len, AwsTemplateProcessor callback = nullptr);
    bool _sourceValid() const
    {
        return true;
    }
    virtual size_t _fillBuffer(uint8_t *buf, size_t maxLen) override;
};

#endif